<?php require_once('../Connections/sepolwan_connect.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable sesi_nama set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "index.php?stat=login_heula";
if (!((isset($_SESSION['sesi_nama'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['sesi_nama'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO gallery (id, foto, judul, idalbum) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($image=date("YmdHis")._.$_FILES['foto']['name'], "text"),
                       GetSQLValueString($_POST['judul'], "text"),
                       GetSQLValueString($_POST['idalbum'], "text"));
					  

 define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;
  
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image=$image;
	$uploadedfile = $_FILES['foto']['tmp_name'];
     
 
 	if ($image) 
 	{
 	
 		$filename = stripslashes($image);
 	
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
		
		
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		
 			$change='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors=1;
 		}
 		else
 		{

 $size=filesize($_FILES['foto']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['foto']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['foto']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);


$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);


$newwidth1=100;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


$filename = "../images/gallery/".$image;

$filename1 = "../images/gallery/small/".$image;



imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}

}

  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
  $Result1 = mysql_query($insertSQL, $sepolwan_connect) or die(mysql_error());

  $insertGoTo = "gallery.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$colname_foto = "-1";
if (isset($_GET['idalbum'])) {
  $colname_foto = $_GET['idalbum'];
}
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_foto = sprintf("SELECT * FROM gallery WHERE idalbum = %s ORDER BY id DESC", GetSQLValueString($colname_foto, "text"));
$foto = mysql_query($query_foto, $sepolwan_connect) or die(mysql_error());
$row_foto = mysql_fetch_assoc($foto);
$totalRows_foto = mysql_num_rows($foto);


$page_row = 20;
	$start = $_GET['start'];
	$start = ($start == '') ? '0' : $start; 
	if ($start >= $totalRows_foto) 
	{
		$start = 0;
		$prev = 0;
		$next = $page_row;
	} elseif ($start >= 0) 
	{
		$start = $start;
		$prev = $start - $page_row;
		$next = $start + $page_row;
	} else {
		$start = 0;
		$prev = 0;
		$next = $page_row;
	} 
	$query_foto = sprintf("SELECT * FROM gallery WHERE idalbum = %s ORDER BY id DESC limit $start, $page_row", GetSQLValueString($colname_foto, "text"));
	$foto = mysql_query($query_foto, $sepolwan_connect) or die(mysql_error());

$colname_catealbum = "-1";
if (isset($_GET['menu'])) {
  $colname_catealbum = $_GET['menu'];
}
$idcolname_catealbum = "-1";
if (isset($_GET['idalbum'])) {
  $idcolname_catealbum = $_GET['idalbum'];
}
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_catealbum = sprintf("SELECT * FROM gallery_album WHERE menu = %s and id = %s ORDER BY id DESC", GetSQLValueString($colname_catealbum, "text"),GetSQLValueString($idcolname_catealbum, "int"));
$catealbum = mysql_query($query_catealbum, $sepolwan_connect) or die(mysql_error());
$row_catealbum = mysql_fetch_assoc($catealbum);
$totalRows_catealbum = mysql_num_rows($catealbum);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet">
</head>

<body>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td class="btnews_bg"><div class="txt14coklatbold" style="margin:10px 20px 10px 20px"><?php echo $row_catealbum['judul']; ?></div></td>
  </tr>
  <tr>
    <td>
    <?php
    $back = "inputalbum.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $back .= (strpos($back, '?')) ? "&" : "?";
    $back .= $_SERVER['QUERY_STRING'];
  } ?>
    <div align="center"><strong style="color:#F00"><br />
        <a href="<?php echo "$back" ?>">&lt;&lt; back</a><a href="inputalbum.php"></a></strong><br /></div>
    <form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
      <table width="100%" align="center">
        <tr valign="baseline">
          <td width="100%"><div style="margin-left:5px">Pilih Image : <input type="file" name="foto" size="32" /></div></td>
        </tr>
        <tr valign="baseline">
          <td width="100%"><input type="text" name="judul" id="nama2" placeholder="Deskripsi Foto . ." value="" size="100%" /></td>
        </tr>
        <tr valign="baseline">
          <td width="100%"><div style="margin-left:5px">Album : <?php echo $row_catealbum['judul']; ?>
            <input type="hidden" name="idalbum" value="<?php echo $row_catealbum['id']; ?>" /></div></td>
        </tr>
        <tr valign="baseline">
          <td width="100%"><input type="submit" value="Simpan" id="masuk" /></td>
        </tr>
      </table>
      <input type="hidden" name="id" value="" />
      <input type="hidden" name="MM_insert" value="form1" />
    </form>
    <p><?php echo $change ?></p>

 <?php  
	  $num = $start;
	  while ($row_foto = mysql_fetch_assoc($foto)){
?><div style="padding:5px; width:150px; float:left;" align="center">
 <img src="../images/gallery/small/<?php echo $row_foto['foto']; ?>" width="150" /><br />
 <a href="delgallery.php?start=<?php echo $_GET['start'] ?>&id=<?php echo $row_foto['id']; ?>">delete</a></div>
            <?php
}
?>
<div align="center"> <table width="103" border="0" align="center" style="margin:10px">
              <tr>
                <?php if ( $prev > -1 and $totalRows_foto > 0 ){ ?><td align="center" id="judul" ><div id="next" align="center"><a href="?start=<?php echo $prev ?>">Pref</a></div></td><?php } ?>
                <?php
			for($i = 0; $i < (round($totalRows_foto / $page_row)); $i++)
				echo "<td align='center' id='judul'> [<a href=\"?start=".($i * $page_row)." \">".($i  + 1)."</a>] </td>";
			?>
                <?php if ( $totalRows_foto > $next ){ ?><td id="judul"><div id="next" align="center"><a href="?start=<?php echo $next ?>">Next</a></div></td><?php } ?>
                </tr>
                </table></div>
</td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($foto);

mysql_free_result($catealbum);
?>
